/**
 * Created by jiangtao on 2017/5/27.
 */

import { bindActionCreators } from 'redux';

import { connect } from 'react-redux';

export default ( options ) => {

    let opts =  { reducers:{}, actions:{} };

    let { reducers , actions } = Object.assign( opts , options );

    return ( component ) => {

        return connect (
            ( state ) => {
                let bindState = {};
                for ( let key in reducers )
                {
                    let stateKey = reducers[key];
                    if( !! state[ stateKey ] ) {
                        bindState[key] = state[ stateKey ];
                    };
                };
                return bindState;
            },
            (dispatch) => {
                let bindAction = {};
                for ( let key in actions )
                {
                    bindAction[key] = bindActionCreators ( actions[key] , dispatch )
                }
                return bindAction;
            }
        ) ( component );
    };
};
